﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TemplateEmailSender
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            Browser.Url = new Uri(TemplatePicker.FileName);
            Template.GetTemplate(TemplatePicker.FileName);
            Template.GetClumnNamesFromTemplate();
            template.Visible = false;
            entries.Visible = true;
            BackToTemplate.Visible = true;

        }

        private void template_Click(object sender, EventArgs e)
        {
            TemplatePicker.ShowDialog();
        }

        private void entries_Click(object sender, EventArgs e)
        {
            EntriesPicker.ShowDialog();
        }

        private void mails_Click(object sender, EventArgs e)
        {
            MailsPicker.ShowDialog();
        }

        private void EntriesPicker_FileOk(object sender, CancelEventArgs e)
        {
            Entry.items.Clear();
            if (Entry.GetEntries(EntriesPicker.FileName))
            {
                entries.Visible = false;
                BackToTemplate.Visible = false;
                mails.Visible = true;
                backToEntries.Visible = true;
            }
        }

        private void MailsPicker_FileOk(object sender, CancelEventArgs e)
        {
            MailsList.mailsList.Clear();
            if (MailsList.GetMails(MailsPicker.FileName))
            {
                mails.Visible = false;
                backToEntries.Visible = false;
                BackToMails.Visible = true;
                label1.Visible = true;
                subject.Visible = true;
            }
        }

        private void sender_Click(object sender, EventArgs e)
        {
            try
            {
                HideAllControls();
                progressBar1.Visible = true;
                percentage.Visible = true;
                backgroundWorker1.RunWorkerAsync();
            }
            catch (Exception)
            {
                InapropriateDataSignal();
            }
            
        }

        private void IfSuccess()
        {
            if (status)
            {
                Browser.Url = new Uri("about:blank");
                MessageBox.Show("Congratulations!!!");
                percentage.Text = "0%";
                progressBar1.Value = 0;
                progressBar1.Visible = false;
                percentage.Visible = false;
                subject.Text = "";
                senderMail.Text = "";
                SMTP.Text = "";
                pass.Text = "";
                this.HideAllControls();
                template.Visible = true;
            }
            else
            {
                InapropriateDataSignal();
            }
        }
       
        private void subject_TextChanged(object sender, EventArgs e)
        {
            label2.Visible = true;
            senderMail.Visible = true;
        }

        private void senderMail_TextChanged(object sender, EventArgs e)
        {
            label3.Visible = true;
            SMTP.Visible = true;
        }

        private void SMTP_TextChanged(object sender, EventArgs e)
        {
            label4.Visible = true;
            pass.Visible = true;
        }

        private void pass_TextChanged(object sender, EventArgs e)
        {
            sendBtn.Visible = true;
            atachment.Visible = true;
        }

        private void BackToMails_Click(object sender, EventArgs e)
        {
            atachment.Visible = false;
            label1.Visible = false;
            subject.Visible = false;
            subject.Text = "";
            label2.Visible = false;
            senderMail.Visible = false;
            senderMail.Text = "";
            label3.Visible = false;
            SMTP.Visible = false;
            SMTP.Text = "";
            label4.Visible = false;
            pass.Visible = false;
            pass.Text = "";
            sendBtn.Visible = false;
            BackToMails.Visible = false;
            mails.Visible = true;
            backToEntries.Visible = true;
        }

        private void backToEntries_Click(object sender, EventArgs e)
        {
            mails.Visible = false;
            backToEntries.Visible = false;
            entries.Visible = true;
            BackToTemplate.Visible = true;
        }

        private void BackToTemplate_Click(object sender, EventArgs e)
        {
            entries.Visible = false;
            BackToTemplate.Visible = false;
            template.Visible = true;
            Browser.Url = new Uri("about:blank");
        }

        private void atachment_Click(object sender, EventArgs e)
        {
            atachFile.ShowDialog();
        }

        private void atachFile_FileOk(object sender, CancelEventArgs e)
        {
             
        }
        private void HideAllControls()
        {
            this.atachment.Visible = false;
            this.label1.Visible = false;
            this.subject.Visible = false;
            this.label2.Visible = false;
            this.senderMail.Visible = false;
            this.label3.Visible = false;
            this.SMTP.Visible = false;
            this.label4.Visible = false;
            this.pass.Visible = false;
            this.sendBtn.Visible = false;
            this.BackToMails.Visible = false;

        }
        private void ShowAllControls()
        {
            this.atachment.Visible = true;
            this.label1.Visible = true;
            this.subject.Visible = true;
            this.label2.Visible = true;
            this.senderMail.Visible = true;
            this.label3.Visible = true;
            this.SMTP.Visible = true;
            this.label4.Visible = true;
            this.pass.Visible = true;
            this.sendBtn.Visible = true;
            this.BackToMails.Visible = true;

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            status = true;
            try
            {
                MailsList.ConstructSMTP(SMTP.Text, senderMail.Text, pass.Text);
                for (int i = 0; i < MailsList.mailsList.Count; i++)
                {
                    int progress = (int)(((decimal)(i+1) / MailsList.mailsList.Count) * 100);
                    MailsList.ConstructMessage(senderMail.Text, subject.Text, atachFile.FileName, i);
                    MailsList.SendMessage();
                    backgroundWorker1.ReportProgress(progress);
                }
            }
            catch (Exception)
            {
                status = false;
                backgroundWorker1.CancelAsync();
            }
        }

        private void InapropriateDataSignal()
        {
            MessageBox.Show("There is an inapropriate data in one of the TextBoxes!!!");
            progressBar1.Visible = false;
            percentage.Visible = false;
            ShowAllControls();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            percentage.Text = e.ProgressPercentage+ "%";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IfSuccess();
        }
    }
}
